home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / man / cat.1 / perlbot.1 < prev    next >
Text File  |  1995-07-25  |  17KB  |  529 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.           perlbot - Bag'o Object Tricks For Perl5 (the BOT)
  10.  
  11.      IIIINNNNTTTTRRRROOOODDDDUUUUCCCCTTTTIIIIOOOONNNN
  12.           The following collection of tricks and hints is intended to
  13.           whet curious appetites about such things as the use of
  14.           instance variables and the mechanics of object and class
  15.           relationships.  The reader is encouraged to consult relevant
  16.           textbooks for discussion of Object Oriented definitions and
  17.           methodology.  This is not intended as a comprehensive guide
  18.           to Perl5's object oriented features, nor should it be
  19.           construed as a style guide.
  20.  
  21.           The Perl motto still holds:  There's more than one way to do
  22.           it.
  23.  
  24.      IIIINNNNSSSSTTTTAAAANNNNCCCCEEEE VVVVAAAARRRRIIIIAAAABBBBLLLLEEEESSSS
  25.           An anonymous array or anonymous hash can be used to hold
  26.           instance variables.  Named parameters are also demonstrated.
  27.  
  28.                   package Foo;
  29.  
  30.                   sub new {
  31.                           my $type = shift;
  32.                           my %params = @_;
  33.                           my $self = {};
  34.                           $self->{'High'} = $params{'High'};
  35.                           $self->{'Low'}  = $params{'Low'};
  36.                           bless $self;
  37.                   }
  38.  
  39.                   package Bar;
  40.  
  41.                   sub new {
  42.                           my $type = shift;
  43.                           my %params = @_;
  44.                           my $self = [];
  45.                           $self->[0] = $params{'Left'};
  46.                           $self->[1] = $params{'Right'};
  47.                           bless $self;
  48.                   }
  49.  
  50.                   package main;
  51.  
  52.                   $a = new Foo ( 'High' => 42, 'Low' => 11 );
  53.                   print "High=$a->{'High'}\n";
  54.                   print "Low=$a->{'Low'}\n";
  55.  
  56.                   $b = new Bar ( 'Left' => 78, 'Right' => 40 );
  57.                   print "Left=$b->[0]\n";
  58.                   print "Right=$b->[1]\n";
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                                          (printed 6/30/95)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  71.  
  72.  
  73.  
  74.      SSSSCCCCAAAALLLLAAAARRRR IIIINNNNSSSSTTTTAAAANNNNCCCCEEEE VVVVAAAARRRRIIIIAAAABBBBLLLLEEEESSSS
  75.           An anonymous scalar can be used when only one instance
  76.           variable is needed.
  77.  
  78.                   package Foo;
  79.  
  80.                   sub new {
  81.                           my $type = shift;
  82.                           my $self;
  83.                           $self = shift;
  84.                           bless \$self;
  85.                   }
  86.  
  87.                   package main;
  88.  
  89.                   $a = new Foo 42;
  90.                   print "a=$$a\n";
  91.  
  92.  
  93.      IIIINNNNSSSSTTTTAAAANNNNCCCCEEEE VVVVAAAARRRRIIIIAAAABBBBLLLLEEEE IIIINNNNHHHHEEEERRRRIIIITTTTAAAANNNNCCCCEEEE
  94.           This example demonstrates how one might inherit instance
  95.           variables from a superclass for inclusion in the new class.
  96.           This requires calling the superclass's constructor and
  97.           adding one's own instance variables to the new object.
  98.  
  99.                   package Bar;
  100.  
  101.                   sub new {
  102.                           my $self = {};
  103.                           $self->{'buz'} = 42;
  104.                           bless $self;
  105.                   }
  106.  
  107.                   package Foo;
  108.                   @ISA = qw( Bar );
  109.  
  110.                   sub new {
  111.                           my $self = new Bar;
  112.                           $self->{'biz'} = 11;
  113.                           bless $self;
  114.                   }
  115.  
  116.                   package main;
  117.  
  118.                   $a = new Foo;
  119.                   print "buz = ", $a->{'buz'}, "\n";
  120.                   print "biz = ", $a->{'biz'}, "\n";
  121.  
  122.  
  123.      OOOOBBBBJJJJEEEECCCCTTTT RRRREEEELLLLAAAATTTTIIIIOOOONNNNSSSSHHHHIIIIPPPPSSSS
  124.           The following demonstrates how one might implement
  125.           "containing" and "using" relationships between objects.
  126.  
  127.  
  128.  
  129.      Page 2                                          (printed 6/30/95)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  137.  
  138.  
  139.  
  140.                   package Bar;
  141.  
  142.                   sub new {
  143.                           my $self = {};
  144.                           $self->{'buz'} = 42;
  145.                           bless $self;
  146.                   }
  147.  
  148.                   package Foo;
  149.  
  150.                   sub new {
  151.                           my $self = {};
  152.                           $self->{'Bar'} = new Bar ();
  153.                           $self->{'biz'} = 11;
  154.                           bless $self;
  155.                   }
  156.  
  157.                   package main;
  158.  
  159.                   $a = new Foo;
  160.                   print "buz = ", $a->{'Bar'}->{'buz'}, "\n";
  161.                   print "biz = ", $a->{'biz'}, "\n";
  162.  
  163.  
  164.      OOOOVVVVEEEERRRRRRRRIIIIDDDDIIIINNNNGGGG SSSSUUUUPPPPEEEERRRRCCCCLLLLAAAASSSSSSSS MMMMEEEETTTTHHHHOOOODDDDSSSS
  165.           The following example demonstrates how one might override a
  166.           superclass method and then call the method after it has been
  167.           overridden.  The Foo::Inherit class allows the programmer to
  168.           call an overridden superclass method without actually
  169.           knowing where that method is defined.
  170.  
  171.                   package Buz;
  172.                   sub goo { print "here's the goo\n" }
  173.  
  174.                   package Bar; @ISA = qw( Buz );
  175.                   sub google { print "google here\n" }
  176.  
  177.                   package Baz;
  178.                   sub mumble { print "mumbling\n" }
  179.  
  180.                   package Foo;
  181.                   @ISA = qw( Bar Baz );
  182.                   @Foo::Inherit::ISA = @ISA;  # Access to overridden methods.
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.      Page 3                                          (printed 6/30/95)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  203.  
  204.  
  205.  
  206.                   sub new { bless [] }
  207.                   sub grr { print "grumble\n" }
  208.                   sub goo {
  209.                           my $self = shift;
  210.                           $self->Foo::Inherit::goo();
  211.                   }
  212.                   sub mumble {
  213.                           my $self = shift;
  214.                           $self->Foo::Inherit::mumble();
  215.                   }
  216.                   sub google {
  217.                           my $self = shift;
  218.                           $self->Foo::Inherit::google();
  219.                   }
  220.  
  221.                   package main;
  222.  
  223.                   $foo = new Foo;
  224.                   $foo->mumble;
  225.                   $foo->grr;
  226.                   $foo->goo;
  227.                   $foo->google;
  228.  
  229.  
  230.      UUUUSSSSIIIINNNNGGGG RRRREEEELLLLAAAATTTTIIIIOOOONNNNSSSSHHHHIIIIPPPP WWWWIIIITTTTHHHH SSSSDDDDBBBBMMMM
  231.           This example demonstrates an interface for the SDBM class.
  232.           This creates a "using" relationship between the SDBM class
  233.           and the new class Mydbm.
  234.  
  235.                   use SDBM_File;
  236.                   use POSIX;
  237.  
  238.                   package Mydbm;
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.      Page 4                                          (printed 6/30/95)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  269.  
  270.  
  271.  
  272.                   sub TIEHASH {
  273.                       my $self = shift;
  274.                       my $ref  = SDBM_File->new(@_);
  275.                       bless {'dbm' => $ref};
  276.                   }
  277.                   sub FETCH {
  278.                       my $self = shift;
  279.                       my $ref  = $self->{'dbm'};
  280.                       $ref->FETCH(@_);
  281.                   }
  282.                   sub STORE {
  283.                       my $self = shift;
  284.                       if (defined $_[0]){
  285.                           my $ref = $self->{'dbm'};
  286.                           $ref->STORE(@_);
  287.                       } else {
  288.                           die "Cannot STORE an undefined key in Mydbm\n";
  289.                       }
  290.                   }
  291.  
  292.                   package main;
  293.  
  294.                   tie %foo, Mydbm, "Sdbm", O_RDWR|O_CREAT, 0640;
  295.                   $foo{'bar'} = 123;
  296.                   print "foo-bar = $foo{'bar'}\n";
  297.  
  298.                   tie %bar, Mydbm, "Sdbm2", O_RDWR|O_CREAT, 0640;
  299.                   $bar{'Cathy'} = 456;
  300.                   print "bar-Cathy = $bar{'Cathy'}\n";
  301.  
  302.  
  303.      TTTTHHHHIIIINNNNKKKKIIIINNNNGGGG OOOOFFFF CCCCOOOODDDDEEEE RRRREEEEUUUUSSSSEEEE
  304.           One strength of Object-Oriented languages is the ease with
  305.           which old code can use new code.  The following examples
  306.           will demonstrate first how one can hinder code reuse and
  307.           then how one can promote code reuse.
  308.  
  309.           This first example illustrates a class which uses a fully-
  310.           qualified method call to access the "private" method _B_A_Z().
  311.           The second example will show that it is impossible to
  312.           override the _B_A_Z() method.
  313.  
  314.                   package FOO;
  315.  
  316.                   sub new { bless {} }
  317.                   sub bar {
  318.                           my $self = shift;
  319.                           $self->FOO::private::BAZ;
  320.                   }
  321.  
  322.                   package FOO::private;
  323.  
  324.  
  325.  
  326.  
  327.      Page 5                                          (printed 6/30/95)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  335.  
  336.  
  337.  
  338.                   sub BAZ {
  339.                           print "in BAZ\n";
  340.                   }
  341.  
  342.                   package main;
  343.  
  344.                   $a = FOO->new;
  345.                   $a->bar;
  346.  
  347.           Now we try to override the _B_A_Z() method.  We would like
  348.           _F_O_O::_b_a_r() to call _G_O_O_P::_B_A_Z(), but this cannot happen since
  349.           _F_O_O::_b_a_r() explicitly calls _F_O_O::_p_r_i_v_a_t_e::_B_A_Z().
  350.  
  351.                   package FOO;
  352.  
  353.                   sub new { bless {} }
  354.                   sub bar {
  355.                           my $self = shift;
  356.                           $self->FOO::private::BAZ;
  357.                   }
  358.  
  359.                   package FOO::private;
  360.  
  361.                   sub BAZ {
  362.                           print "in BAZ\n";
  363.                   }
  364.  
  365.                   package GOOP;
  366.                   @ISA = qw( FOO );
  367.                   sub new { bless {} }
  368.  
  369.                   sub BAZ {
  370.                           print "in GOOP::BAZ\n";
  371.                   }
  372.  
  373.                   package main;
  374.  
  375.                   $a = GOOP->new;
  376.                   $a->bar;
  377.  
  378.           To create reusable code we must modify class FOO, flattening
  379.           class FOO::private.  The next example shows a reusable class
  380.           FOO which allows the method _G_O_O_P::_B_A_Z() to be used in place
  381.           of _F_O_O::_B_A_Z().
  382.  
  383.                   package FOO;
  384.  
  385.                   sub new { bless {} }
  386.                   sub bar {
  387.                           my $self = shift;
  388.                           $self->BAZ;
  389.                   }
  390.  
  391.  
  392.  
  393.      Page 6                                          (printed 6/30/95)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  401.  
  402.  
  403.  
  404.                   sub BAZ {
  405.                           print "in BAZ\n";
  406.                   }
  407.  
  408.                   package GOOP;
  409.                   @ISA = qw( FOO );
  410.  
  411.                   sub new { bless {} }
  412.                   sub BAZ {
  413.                           print "in GOOP::BAZ\n";
  414.                   }
  415.  
  416.                   package main;
  417.  
  418.                   $a = GOOP->new;
  419.                   $a->bar;
  420.  
  421.  
  422.      CCCCLLLLAAAASSSSSSSS CCCCOOOONNNNTTTTEEEEXXXXTTTT AAAANNNNDDDD TTTTHHHHEEEE OOOOBBBBJJJJEEEECCCCTTTT
  423.           Use the object to solve package and class context problems.
  424.           Everything a method needs should be available via the object
  425.           or should be passed as a parameter to the method.
  426.  
  427.           A class will sometimes have static or global data to be used
  428.           by the methods.  A subclass may want to override that data
  429.           and replace it with new data.  When this happens the
  430.           superclass may not know how to find the new copy of the
  431.           data.
  432.  
  433.           This problem can be solved by using the object to define the
  434.           context of the method.  Let the method look in the object
  435.           for a reference to the data.  The alternative is to force
  436.           the method to go hunting for the data ("Is it in my class,
  437.           or in a subclass?  Which subclass?"), and this can be
  438.           inconvenient and will lead to hackery.  It is better to just
  439.           let the object tell the method where that data is located.
  440.  
  441.                   package Bar;
  442.  
  443.                   %fizzle = ( 'Password' => 'XYZZY' );
  444.  
  445.                   sub new {
  446.                           my $self = {};
  447.                           $self->{'fizzle'} = \%fizzle;
  448.                           bless $self;
  449.                   }
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.      Page 7                                          (printed 6/30/95)
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.      PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  467.  
  468.  
  469.  
  470.                   sub enter {
  471.                           my $self = shift;
  472.  
  473.                           # Don't try to guess if we should use %Bar::fizzle
  474.                           # or %Foo::fizzle.  The object already knows which
  475.                           # we should use, so just ask it.
  476.                           #
  477.                           my $fizzle = $self->{'fizzle'};
  478.  
  479.                           print "The word is ", $fizzle->{'Password'}, "\n";
  480.                   }
  481.  
  482.                   package Foo;
  483.                   @ISA = qw( Bar );
  484.  
  485.                   %fizzle = ( 'Password' => 'Rumple' );
  486.  
  487.                   sub new {
  488.                           my $self = Bar->new;
  489.                           $self->{'fizzle'} = \%fizzle;
  490.                           bless $self;
  491.                   }
  492.  
  493.                   package main;
  494.  
  495.                   $a = Bar->new;
  496.                   $b = Foo->new;
  497.                   $a->enter;
  498.                   $b->enter;
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.      Page 8                                          (printed 6/30/95)
  526.  
  527.  
  528.  
  529.